//
//  HcolorPicker.m
//  HMusicPlayer
//
//  Created by Hament on 14/10/14.
//  Copyright (c) 2014年 HJ. All rights reserved.
//

#import "HcolorPicker.h"
#import "ColorView.h"
@interface HcolorPicker () 

@end

@implementation HcolorPicker

@synthesize redStepSize = _redStepSize;
@synthesize greenStepSize;
@synthesize blueStepSize;
@synthesize whiteStepSize;
@synthesize alphaStepSize;


//- (void)viewDidLoad {
//    [super viewDidLoad];
//    // Do view setup here.
//}


-(void)awakeFromNib {

    self.alphaStepSize = 1.0;
    colorView.alpha = self.alphaStepSize;


}

- (IBAction)sliderChanged:(id)sender
{
    // make this static so that if the alpha level is changed it knows
    // whether to modify color or grayscale settings
    static BOOL isColor = NO;
    
    // RGB and WHITE are handled seperatly so if one is
    // set, reset the other. RGB is color, WHITE is grayscale
    if (red == sender) {
        //NSLog(@"slider r changed to %f", rStepSize);
        self.whiteStepSize = 0.0;
        colorView.white   = self.whiteStepSize;
        isColor = YES;
        colorView.red = self.redStepSize;
    }
    if (green == sender) {
        //NSLog(@"slider g changed to %f", gStepSize);
        self.whiteStepSize = 0.0;
        colorView.white   = self.whiteStepSize;
        isColor = YES;
        colorView.green = greenStepSize;
    }
    if (blue == sender) {
        //NSLog(@"slider b changed to %f", bStepSize);
        self.whiteStepSize = 0.0;
        colorView.white   = self.whiteStepSize;
        isColor = YES;
        colorView.blue = blueStepSize;
    }
    if (white == sender) {
        //NSLog(@"slider w changed to %f", wStepSize);
        self.redStepSize = 0.0;
        self.greenStepSize = 0.0;
        self.blueStepSize = 0.0;
        
        colorView.red   = self.redStepSize;
        colorView.green = self.greenStepSize;
        colorView.blue  = self.blueStepSize;
        isColor = NO;
        colorView.white = whiteStepSize;
    }
    if (alpha == sender) {
        //NSLog(@"slider a changed to %f", aStepSize);
        colorView.alpha = alphaStepSize;
    }
    
    if (isColor == YES) {
        [colorCode setStringValue:@""];
        [colorCode setStringValue:[NSString stringWithFormat:@"[NSColor colorWithCalibratedRed:%.2f green:%.2f blue:%.2f alpha:%.2f]",
                                   self.redStepSize,
                                   self.greenStepSize,
                                   self.blueStepSize,
                                   self.alphaStepSize]];
    }
    
    if (isColor == NO) {
        [colorCode setStringValue:@""];
        [colorCode setStringValue:[NSString stringWithFormat:@"[NSColor colorWithCalibratedWhite:%.2f alpha:%.2f]", 
                                   self.whiteStepSize,
                                   self.alphaStepSize]];
    }
    
    
    //动态更改
    if ([self.delegate respondsToSelector:@selector(colorDynamicChange:)]) {
        
        NSColor * color=[NSColor colorWithCalibratedRed:colorView.red green:colorView.green blue:colorView.blue alpha:colorView.alpha];
        [self.delegate colorDynamicChange:color];
    }
    
    
}


//保存
-(IBAction)save:(NSButton*)sender{
    if ([self.delegate respondsToSelector:@selector(selectColor:)]) {
        
        NSString * cv;
        if ([self.tabview selectedTabViewItem]==self.rgbtabviewItem) {
            cv=[NSString stringWithFormat:@"%f,%f,%f,%f",self.redStepSize,self.greenStepSize,self.blueStepSize,self.alphaStepSize];
            
        } else {

             cv=[NSString stringWithFormat:@"%f,%f,%f,%f",self.whiteStepSize,self.whiteStepSize,self.whiteStepSize,self.alphaStepSize];
            //debugLog(@"cv:%@",cv);
        }
        

        
        [self.delegate selectColor:cv];
    }


}


//外部调用
-(void)configWithColor:(NSColor*)color{

    self.redStepSize=color.redComponent;
    self.greenStepSize=color.greenComponent;
    self.blueStepSize=color.blueComponent;
    self.alphaStepSize=color.alphaComponent;
    
    colorView.red = self.redStepSize;
    colorView.green = self.greenStepSize;
    colorView.blue = self.blueStepSize;
    colorView.alpha = self.alphaStepSize;
    colorView.white=0.0;
    
    [self.tabview selectTabViewItem:self.rgbtabviewItem];

}


@end
